<template>
    <el-dialog v-if="dialogVisible" :close-on-click-modal='false' title="权限设置" :visible.sync="dialogVisible" @close="resetForm('memberJurForm')" width="400px">
        <el-form label-position="top" :model="memberJurForm" :hide-required-asterisk='true' ref="memberJurForm" :rules="memberJurRules" class="dialog_form">
            <el-form-item prop="type" style="margin-bottom:10px">
                <el-radio-group v-model="memberJurForm.type">
                    <el-radio :label="1">添加权限</el-radio>
                    <el-radio :label="2">删减权限</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="选择权限" prop="authorize">
                <el-select v-model="memberJurForm.authorize" multiple collapse-tags placeholder="请选择" style="width:100%">
                    <el-option v-for="item in authList" :key="item.id" :label="item.title" :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button size="small" @click="resetForm('memberJurForm')" class="el-button-width">取 消</el-button>
            <el-button size="small" type="primary" @click="submitForm('memberJurForm')" :loading="loading" :disabled="loading" class="el-button-width">保存设置</el-button>
        </span>
    </el-dialog>
</template>

<script>
export default {
    name: "",
    data() {
        return {
            loading: false,
            dialogVisible: false,
            authList:[],
            memberJurForm: {
                type: 1,
                authorize: "",
                users: [],
            },
            memberJurRules: {
                authorize: [
                    {
                        required: true,
                        message: "请选择权限组名称",
                        trigger: ['blur', 'change']
                    },
                ],
            },
        };
    },
    created() {
        this.$bus.on("memberJur", (value) => {
            this.dialogVisible = value.dialogVsb;
            this.memberJurForm.users = value.users
            this.getAuthList()
        });
    },
    beforeDestroy() {
        this.$bus.off("memberJur");
    },
    methods: {
        //获取角色列表
        async getAuthList() {
            const { data: res } = await this.$http.post(
                "adminapi/Jwtauth/user_auth_list"
            );
            if (res.code !== 200) {
                this.$message.error(res.msg);
                return;
            } else {
                this.authList = res.data.list;
            }
        },

        // 提交添加
        submitForm(formName) {
            this.$refs[formName].validate(async (valid) => {
                if (valid) {
                    this.loading = true;
                    const { data: res } = await this.$http.post(
                        "adminapi/company/userSetAuthBatch",
                        this.memberJurForm
                    );
                    if (res.code !== 200) {
                        this.$message.error(res.msg);
                        this.loading = false;
                        return;
                    } else {
                        this.$message({
                            message: "设置成功",
                            type: "success",
                        });
                        this.loading = false;
                        this.resetForm(formName);
                        this.$parent.getDataList();
                    }
                } else {
                    this.loading = false;
                    return false;
                }
            });
        },

        // 关闭重置表单
        resetForm(formName) {
            this.$refs[formName].resetFields();
            this.dialogVisible = false;
        },
    },
    components: {},
};
</script>


<style scoped>
</style>
